package com.yuyang.wifi.utils;

import java.util.List;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/8/16
 *     desc  : 字符串相关工具类
 * </pre>
 */
public class StringUtils {

    private StringUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 判断字符串是否为null或长度为0
     *
     * @param s 待校验字符串
     * @return {@code true}: 空<br> {@code false}: 不为空
     */
    public static boolean isEmpty(CharSequence s) {
        return s == null || s.length() == 0;
    }

    /**
     * 判断字符串是否为null或全为空格
     *
     * @param s 待校验字符串
     * @return {@code true}: null或全空格<br> {@code false}: 不为null且不全空格
     */
    public static boolean isSpace(String s) {
        return (s == null || s.trim().length() == 0);
    }

    /**
     * 判断两字符串是否相等
     *
     * @param a 待校验字符串a
     * @param b 待校验字符串b
     * @return {@code true}: 相等<br>{@code false}: 不相等
     */
    public static boolean equals(CharSequence a, CharSequence b) {
        if (a == b) return true;
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i)) return false;
                }
                return true;
            }
        }
        return false;
    }

    /**
     * 判断两字符串忽略大小写是否相等
     *
     * @param a 待校验字符串a
     * @param b 待校验字符串b
     * @return {@code true}: 相等<br>{@code false}: 不相等
     */
    public static boolean equalsIgnoreCase(String a, String b) {
        return (a == b) || (b != null) && (a.length() == b.length()) && a.regionMatches(true, 0, b, 0, b.length());
    }

    /**
     * null转为长度为0的字符串
     *
     * @param s 待转字符串
     * @return s为null转为长度为0字符串，否则不改变
     */
    public static String null2Length0(String s) {
        return s == null ? "" : s;
    }

    /**
     * 返回字符串长度
     *
     * @param s 字符串
     * @return null返回0，其他返回自身长度
     */
    public static int length(CharSequence s) {
        return s == null ? 0 : s.length();
    }

    /**
     * 首字母大写
     *
     * @param s 待转字符串
     * @return 首字母大写字符串
     */
    public static String upperFirstLetter(String s) {
        if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) return s;
        return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1);
    }

    /**
     * 首字母小写
     *
     * @param s 待转字符串
     * @return 首字母小写字符串
     */
    public static String lowerFirstLetter(String s) {
        if (isEmpty(s) || !Character.isUpperCase(s.charAt(0))) {
            return s;
        }
        return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1);
    }

    /**
     * 反转字符串
     *
     * @param s 待反转字符串
     * @return 反转字符串
     */
    public static String reverse(String s) {
        int len = length(s);
        if (len <= 1) return s;
        int mid = len >> 1;
        char[] chars = s.toCharArray();
        char c;
        for (int i = 0; i < mid; ++i) {
            c = chars[i];
            chars[i] = chars[len - i - 1];
            chars[len - i - 1] = c;
        }
        return new String(chars);
    }

    /**
     * 转化为半角字符
     *
     * @param s 待转字符串
     * @return 半角字符串
     */
    public static String toDBC(String s) {
        if (isEmpty(s)) {
            return s;
        }
        char[] chars = s.toCharArray();
        for (int i = 0, len = chars.length; i < len; i++) {
            if (chars[i] == 12288) {
                chars[i] = ' ';
            } else if (65281 <= chars[i] && chars[i] <= 65374) {
                chars[i] = (char) (chars[i] - 65248);
            } else {
                chars[i] = chars[i];
            }
        }
        return new String(chars);
    }

    /**
     * 转化为全角字符
     *
     * @param s 待转字符串
     * @return 全角字符串
     */
    public static String toSBC(String s) {
        if (isEmpty(s)) {
            return s;
        }
        char[] chars = s.toCharArray();
        for (int i = 0, len = chars.length; i < len; i++) {
            if (chars[i] == ' ') {
                chars[i] = (char) 12288;
            } else if (33 <= chars[i] && chars[i] <= 126) {
                chars[i] = (char) (chars[i] + 65248);
            } else {
                chars[i] = chars[i];
            }
        }
        return new String(chars);
    }
    public static String getGoldString(long gold){
        try {
            if(Math.abs(gold) > 100000){
                String setCoinStr = setCoinStr(String.valueOf(gold / 10000));
                return setCoinStr +"w";
            }
        }catch (ClassCastException e){

        }
        return setCoinStr(""+gold);
    }
    public static String getGoldString(String gold){
        try {
            if(Math.abs(Double.valueOf(gold)) > 100000){
                double setCoinStr = Double.valueOf(gold) / 10000;
                setCoinStr = (double)Math.round(setCoinStr*100)/100;
                return setCoinStr +"w";
            }
        }catch (ClassCastException e){
        }
        return gold;
    }
    //给大数字后面的0加逗号的方法（比如120,000,000）
    public static String setCoinStr(String str){
//        if(str.length()==4){
//            if(str.startsWith("-")){
//                return str;
//            }
//        }
        if(str.startsWith("-")){
            String substring = str.substring(1);
            StringBuffer temp0=new StringBuffer(substring).reverse();

            StringBuffer resultStr0=new StringBuffer(temp0);
            for (int i = 3; i <resultStr0.length() ; i+=4) {
                resultStr0.insert(i,",");
            }

            return "-"+resultStr0.reverse().toString();
        }
        StringBuffer temp=new StringBuffer(str).reverse();

        StringBuffer resultStr=new StringBuffer(temp);
        for (int i = 3; i <resultStr.length() ; i+=4) {
            resultStr.insert(i,",");
        }

        return resultStr.reverse().toString();
    }
    public static String listToString(List<String> stringList){
        if (stringList == null) {
            return null;
        }
        StringBuilder result=new StringBuilder();
        boolean flag=false;
        for (String string : stringList) {
            if (flag) {
                result.append(","); // 分隔符
            }else {
                flag=true;
            }
            result.append(string);
        }
        return result.toString();
    }
    //把手机号字符串的4-7位数字变为*
    public static String setPhoneStr(String phoneNum){
        StringBuffer resultStr=new StringBuffer();
        for(int i=0;i<phoneNum.length();++i){
            if(i==3||i==4||i==5||i==6){
                resultStr.append("*");
            }else{
                resultStr.append(phoneNum.charAt(i));
            }
        }
        return resultStr.toString();
    }
    //把银行卡号第5-8位数字变为*
    public static String setBankNumStr(String bankNum){
        StringBuffer resultStr=new StringBuffer();
        String substring = bankNum.substring(bankNum.length() - 5, bankNum.length() - 1);
        for (int i = 0; i < 4; i++) {
            resultStr.append("**** ");
        }
        if(substring.contains(" ")){
            for (int i = 0; i < bankNum.length(); ++i) {
                if (i == bankNum.length() - 1 || i == bankNum.length() - 2 || i == bankNum.length() - 3 || i == bankNum.length() - 4||i==bankNum.length()-5) {
                    if (bankNum.charAt(i) != ' ') {
                        resultStr.append(bankNum.charAt(i));
                    }else {
                        resultStr.append("");
                    }
                }
            }
            return resultStr.toString();
        }else {
            for (int i = 0; i < bankNum.length(); ++i) {
                if (i == bankNum.length() - 1 || i == bankNum.length() - 2 || i == bankNum.length() - 3 || i == bankNum.length() - 4) {
                    if (bankNum.charAt(i) == ' ') {
                        resultStr.append(" ");
                    } else {
                        resultStr.append(bankNum.charAt(i));
                    }
                }
            }
            return resultStr.toString();
        }
    }
    //把身份证号第5-8位数字变为*
    public static String setIdCardNumStr(String bankNum){
        StringBuffer resultStr=new StringBuffer();
        for(int i=0;i<bankNum.length();++i){
            if(i==6||i==7||i==8||i==9){
                resultStr.append("*");
            }else if(i==10||i==11||i==12||i==13){
            } else {
                resultStr.append(bankNum.charAt(i));
            }
        }
        return resultStr.toString();
    }


    // 隐藏字符串 隔一个变成*   唐*兰*阁*店
    public static String getHideString(String str){
        String temp = str.trim();
        StringBuilder result = new StringBuilder();
        for(int i = 0 ; i < temp.trim().length();i++){
            if(i%2 == 0){
                result.append(temp.charAt(i));
            }else{
                result.append("▒");
            }
        }

        return  result.toString();
    }
}